<template>
  <div class="shoprule">
    规格搜索:
    <el-select
      v-model="value9"
      multiple
      filterable
      remote
      reserve-keyword
      placeholder="请输入关键词"
      :remote-method="remoteMethod"
      :loading="loading"
    >
      <el-option
        v-for="item in options4"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>

    <p class="table_top_btn">
      <el-button
        type="text"
        @click="addShop"
      >
        添加商品规格
      </el-button>

      <el-dialog
        title="属性规格"
        :visible.sync="dialogFormVisible"
      >
        <el-form :model="form">
          <el-form-item
            label="规格名称"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="form.title"
              auto-complete="off"
              placeholder="请输入标题名称"
            />
          </el-form-item>

          <el-form-item
            label="规格"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="form.name"
              auto-complete="off"
              placeholder="请输入规格"
            />
          </el-form-item>
          <el-form-item
            label="规格值"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="form.text"
              auto-complete="off"
              placeholder="请输入规格值"
            />
          </el-form-item>
        </el-form>
        <div
          slot="footer"
          class="dialog-footer"
        >
          <el-button @click="dialogFormVisible = false">
            取 消
          </el-button>
          <el-button
            type="text"
            @click="submit"
          >
            确 定
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        title="属性规格"
        :visible.sync="dialogToVisible"
      >
        <el-form :model="form">
          <el-form-item
            label="规格名称"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="form.name1"
              auto-complete="off"
              placeholder="请输入标题名称"
            />
          </el-form-item>
        </el-form>
        <p>消耗</p>
        <p class="but_btn">
          <el-button>500W</el-button>
          <el-button>+添加</el-button>
        </p>

        <el-button type="primary">
          添加新规格
        </el-button>
        <div
          slot="footer"
          class="dialog-footer"
        >
          <el-button @click="dialogToVisible = false">
            取 消
          </el-button>
          <el-button
            type="primary"
            @click="dialogToVisible = false"
          >
            确 定
          </el-button>
        </div>
      </el-dialog>

      <el-button>批量删除</el-button>
    </p>

    <div class="table">
      <el-table
        :default-sort="{prop: 'id',order:'descending'}"
        :data="tableData"
        border
        style="width: 100%;"
      >
        <el-table-column
          type="selection"
          width="100"
        />
        <el-table-column
          prop="id"
          label="ID"
          width="200"
        />
        <el-table-column
          prop="name"
          label="规格名称"
          width="200"
        />
        <el-table-column
          prop="title"
          label="商品规格"
          width="150"
        />
        <el-table-column
          prop="text"
          label="商品属性"
          width="150"
        />
        <el-table-column
          fixed="right"
          label="操作"
          width="220"
        >
          <template slot-scope="scope">
            <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            > -->
            <el-button
              type="text"
              @click="editShop(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click.native.prevent="deleteRow(scope.$index, tableData)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
  
<script>

// import axios from 'axios'
import {apiGetSpecification} from '@/api/specification.js'
export default {
  data() {
    return {
      search: "",
      searchData: "",
      tableData: [],
      options4: [],
      value9: [],
      list: [],
      loading: false,
      states: [],

      dialogFormVisible: false,
      dialogToVisible: false,
      dialogInVisible: false,
      form: {
        name: "",
       title:'',
       text:''
      },
      formLabelWidth: "120px",
    //  cur_page:1,//默认在第一页

      pageIndex:1,//起始页
      pageSize:10,//每页显示
      total:0,//默认总条数为0条
      options:''
    };
  },
  created() {
   this.getSpecification()
  },

  mounted() {
    this.list = this.states.map(item => {
      return { value: item, label: item };
    });
  },
  methods: {
    submit(){
       this.dialogFormVisible=false;
       this.dialogToVisible=true;
       if(this.options==='add'){
         if(this.form.name!==''&&this.form.title!==''&&this.form.text!==''){
              this.tableData.push(this.form)
              console.log(this.tableData,'tttt')
         }else{
           alert('不能为空')
         }
          this.form={
           name: "",
          title:'',
          text:''
         }
          
       }else if(this.options==='edit'){
         this.tableData.forEach(item=>{
            if(item.id===this.form.id){
                item.name=this.form.name
                item.title=this.form.title
                item.text=this.form.text
            }
         })
         this.form={
           name: "",
          title:'',
          text:''
         }
         
       }
    },
    
    //  indexMethod(id) {
    //     return id;
    //   },
   
    handleCurrentChange(val) {
      console.log(this.pageIndex,'cccccc')
        this.pageIndex = val;
        this.getSpecification();//获取用户点击的当前页后刷新页面数据
    }, 
    
                
    handleEdit(index, row) {
      console.log(index, row);
    },
    addShop(){
      this.options='add'
       this.dialogFormVisible=true;
      //  this.dialogToVisible=true;
      
    },
    editShop(row){
      this.options='edit'
       this.dialogFormVisible=true;
      //  this.dialogToVisible=true;
       this.form={...row}
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        setTimeout(() => {
          this.loading = false;
          this.options4 = this.list.filter(item => {
            return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
          });
        }, 200);
      } else {
        this.options4 = [];
      }
    },
    // pageChange(ind) {
     
     
    // },
    async getSpecification(){
      const res=await apiGetSpecification({
        pageIndex:this.pageIndex,
        pageSize:this.pageSize
      })
      console.log(res,'sssss')
      this.tableData=res.data;
      this.total=res.total;
    },
    handleClick(tab, event) {
      console.log(tab, event);
    }
  }
};
</script>
<style lang="scss" scoped>
.shoprule {
  background: #fff;
  padding: 20px;
}
.table_top_btn {
  margin: 20px 0;
  button {
    margin: 0 10px;
  }

}
.table {
  width: 100%;
  margin: 30px 0;
}

.but_btn {
  margin: 20px 0;
}
</style>
